package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Personinfo;
import com.ruoyi.system.service.IPersoninfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PersonHouse;
import com.ruoyi.system.service.IPersonHouseService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 房产信息Controller
 * 
 * @author TYF
 * @date 2023-08-12
 */
@Controller
@RequestMapping("/system/house")
public class PersonHouseController extends BaseController
{
    private String prefix = "system/house";

    @Autowired
    private IPersonHouseService personHouseService;
    @Autowired
    private IPersoninfoService personinfoService;

    @RequiresPermissions("system:house:view")
    @GetMapping()
    public String house()
    {
        return prefix + "/house";
    }

    /**
     * 查询房产信息列表
     */
    @RequiresPermissions("system:house:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PersonHouse personHouse)
    {
        try{
            if(StringUtils.isNotEmpty(personHouse.getName())){
                Personinfo personinfo =new Personinfo();
                personinfo.setName(personHouse.getName());
                List<Personinfo> personinfos = personinfoService.selectPersoninfoList(personinfo);
                personHouse.setPid(personinfos.get(0).getId());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        startPage();
        List<PersonHouse> list = personHouseService.selectPersonHouseList(personHouse);
        for(PersonHouse house:list){
            String name = personinfoService.selectPersoninfoById(house.getPid()).getName();
            house.setName(name);
        }
        return getDataTable(list);
    }

    /**
     * 导出房产信息列表
     */
    @RequiresPermissions("system:house:export")
    @Log(title = "房产信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PersonHouse personHouse)
    {
        List<PersonHouse> list = personHouseService.selectPersonHouseList(personHouse);
        ExcelUtil<PersonHouse> util = new ExcelUtil<PersonHouse>(PersonHouse.class);
        return util.exportExcel(list, "房产信息数据");
    }

    /**
     * 新增房产信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存房产信息
     */
    @RequiresPermissions("system:house:add")
    @Log(title = "房产信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PersonHouse personHouse)
    {
        return toAjax(personHouseService.insertPersonHouse(personHouse));
    }

    /**
     * 修改房产信息
     */
    @RequiresPermissions("system:house:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        PersonHouse personHouse = personHouseService.selectPersonHouseById(id);
        mmap.put("personHouse", personHouse);
        return prefix + "/edit";
    }

    /**
     * 修改保存房产信息
     */
    @RequiresPermissions("system:house:edit")
    @Log(title = "房产信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PersonHouse personHouse)
    {
        return toAjax(personHouseService.updatePersonHouse(personHouse));
    }

    /**
     * 删除房产信息
     */
    @RequiresPermissions("system:house:remove")
    @Log(title = "房产信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(personHouseService.deletePersonHouseByIds(ids));
    }
}
